package com.sx.service.serviceImpl;

import com.sx.dao.SaleBillDao;
import com.sx.dao.daoImpl.SaleBillDaoImpl;
import com.sx.pojo.Goods;
import com.sx.pojo.OptionRecords;
import com.sx.pojo.SaleBill;
import com.sx.service.GoodService;
import com.sx.service.OptionRecordService;
import com.sx.service.SaleBillService;

import java.sql.SQLException;
import java.util.ArrayList;

public class SaleBillServiceImpl implements SaleBillService {
    private SaleBillDao saleBillDao = new SaleBillDaoImpl();
    private GoodService goodService = new GoodServiceImpl();
    private OptionRecordService optionRecordService = new OptionRecordServiceImpl();

    @Override
    public ArrayList<SaleBill> getAllSaleBill() {
        return saleBillDao.getAllSaleBill();
    }

    @Override
    public boolean checkSaleBillIDExists(String saleBillID) {
        return saleBillDao.checkSaleBillIDExists(saleBillID);
    }

    @Override
    public int saveSaleBill(SaleBill saleBill, String PersonId, String PersonName, int flag, int type) {
        //对商品剩余数量判读
        if (goodService.getGoodById(saleBill.getGoods_id()).getNumber() > saleBill.getNumber()) {
            //执行减少商品数量的操作
            Goods goods = goodService.getGoodById(saleBill.getGoods_id());
            goods.setNumber(goods.getNumber() - saleBill.getNumber());
            boolean b = goodService.updateGood(goods);
            if (b) {
                if (saleBillDao.saveSaleBill(saleBill)) {
                    optionRecordService.addOptionRecord(PersonId, PersonName, saleBill.toString(), null, flag, type);
                    return 1;
                } else {
                    return 3;
                }
            } else {
                return 3;
            }
        } else {
            return 2;
        }

    }

    /*@Override
    public boolean deleteSaleBillById(String saleBillID) {
        return saleBillDao.deleteSaleBillById(saleBillID);
    }*/
    public boolean deleteSaleBillById(String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords OptionRecords = new OptionRecords();
        OptionRecords.setPersonId(PersonId);
        OptionRecords.setPersonName(PersonName);
        OptionRecords.setFlag(flag);
        OptionRecords.setType(type);
        OptionRecords.setBeOption(saleBillDao.getSaleBillById(id).toString());
        OptionRecords.setAfOption(null);
        if (saleBillDao.deleteSaleBillById(id)) {
            optionRecordService.addOptionRecord(OptionRecords.getPersonId(), OptionRecords.getPersonName(), OptionRecords.getBeOption(), OptionRecords.getAfOption(), OptionRecords.getFlag(), OptionRecords.getType());
            return true;
        } else {
            return false;
        }

    }

    @Override
    public SaleBill getSaleBillById(String saleBillID) {
        return saleBillDao.getSaleBillById(saleBillID);
    }

    @Override
    public boolean updateSaleBill(SaleBill saleBill, String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords OptionRecords = new OptionRecords();
        OptionRecords.setPersonId(PersonId);
        OptionRecords.setPersonName(PersonName);
        OptionRecords.setFlag(flag);
        OptionRecords.setType(type);
        OptionRecords.setBeOption(saleBillDao.getSaleBillById(id).toString());
        OptionRecords.setAfOption(saleBill.toString());
        if (saleBillDao.updateSaleBill(saleBill)) {
            optionRecordService.addOptionRecord(OptionRecords.getPersonId(), OptionRecords.getPersonName(), OptionRecords.getBeOption(), OptionRecords.getAfOption(), OptionRecords.getFlag(), OptionRecords.getType());
            return true;
        } else {
            return false;
        }
    }
}
